﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using Model;

namespace TanHungLong
{
    public partial class FrmKhachHang : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private KhachHangBOCollection KhachHangBOCollection = null;

        private string COLUMN_FIRST = "MaKhachHang";
        private string COLUMN_SECOND = "Ten";
        #endregion variant

        //=====================================
        #region form
        public FrmKhachHang()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmKhachHang_Load);
            this.Shown += new EventHandler(FrmKhachHang_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmKhachHang_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdView.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
            this.grdView.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.KeyDown += new KeyEventHandler(FrmKhachHang_KeyDown);
        }

        private void FrmKhachHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnSave.Visible && ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6)))
                        Save();
                    else
                        if (this.btnEdit.Visible && ((e.KeyCode == Keys.E && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F2)))
                            Edit();
                        else
                            if (this.btnCancel.Visible && ((e.KeyCode == Keys.H && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F12)))
                                Cancel();
        }

        private void FrmKhachHang_Shown(object sender, EventArgs e)
        {
            this.grdView.Focus();
        }

        private void FrmKhachHang_Load(object sender, EventArgs e)
        {
            RefreshData();
            SetFormStatus(this.formStatus);
            Utility.InitControl(this.repLookUpNhomKhachHangID);
        }

        private void FrmKhachHang_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = this.grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && this.grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdView.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            this.KhachHangBOCollection = KhachHangBOCollection.GetKhachHangBOCollection("", "Ten");
            this.bdSource.DataSource = this.KhachHangBOCollection.Clone();
            #region
            DanhMucBOList nhomKhachHangBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.NhomKhachHang);
            Utility.SetData(this.repLookUpNhomKhachHangID, nhomKhachHangBOList, nhomKhachHangBOList.Count);
            #endregion BOList

        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void Save()
        {
            string oldColumn = this.grdView.FocusedColumn.FieldName;
            string newColumn = (oldColumn == this.COLUMN_FIRST ? this.COLUMN_SECOND : this.COLUMN_FIRST);
            this.grdView.FocusedColumn = this.grdView.Columns[newColumn];

            this.bdSource.EndEdit();
            this.KhachHangBOCollection = (KhachHangBOCollection)this.bdSource.DataSource;

            this.KhachHangBOCollection = this.KhachHangBOCollection.Save();
            this.bdSource.DataSource = this.KhachHangBOCollection.Clone();

            this.grdView.FocusedColumn = this.grdView.Columns[oldColumn];
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;
            this.bdSource.DataSource = this.KhachHangBOCollection.Clone();
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            MyStyle.SetGridStyle(this.grdView, edit);

            this.btnEdit.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;

            this.colDELE.Visible = edit;
            this.colSTT0.OptionsColumn.AllowFocus = !edit;
            //this.colMaHang.OptionsColumn.AllowFocus = !edit;
            foreach (GridColumn col in this.grdView.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }

            this.grdView.OptionsView.NewItemRowPosition = (edit ? NewItemRowPosition.Bottom : NewItemRowPosition.None);
            this.grdView.Focus();

            if (edit)
            {
                string focusColumn = this.grdView.FocusedColumn.FieldName;

                if (string.IsNullOrEmpty(focusColumn))
                    focusColumn = this.COLUMN_FIRST;
                else
                    if (focusColumn == "DELE")
                        focusColumn = this.COLUMN_FIRST;

                if (this.grdView.RowCount == 1)
                    this.grdView.FocusedRowHandle = int.MinValue + 1;

                this.grdView.FocusedColumn = this.grdView.Columns[focusColumn];
                this.grdView.ShowEditor();
            }
        }
        #endregion function
    }
}